<template>
  <el-form>
    <el-form-item label="旧密码">
      <el-input type="password" show-password v-model.trim="passwords.oldPassword" placeholder="请输入旧密码"/>
    </el-form-item>
    <el-form-item label="新密码">
      <el-input type="password" show-password v-model.trim="passwords.newPassword" placeholder="请输入新密码"/>
    </el-form-item>
    <el-form-item label="确认密码">
      <el-input type="password"  show-password v-model.trim="confirmPassword" placeholder="请输入确认密码"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<script>

export default {
  props: {
    userInfo: {
      type: Object,
      default: () => {
        return {
          
        }
      }
    }
  },
  data(){
    return {
      passwords:{
        oldPassword: "",
        newPassword: ""
      },
      confirmPassword:"" //确认密码
    }
  },
  methods: {
    submit () {
       if( this.passwords.newPassword !== this.confirmPassword){
         this.$message.error("新密码和确认密码不一致o(╥﹏╥)o！！");
         return false;
       }else{
         if(this.userInfo.role === 1){
            this.putRequest("/api/admin/updatePassword",this.passwords).then(resp => {
              if(resp && resp.code == 200){
                this.passwords = {
                  oldPassword: "",
                  newPassword: "",
                };
                this.confirmPassword = "";
                //退出登录
                //清空用户登录
                  window.sessionStorage.removeItem("tokenStr");
                  window.sessionStorage.removeItem("user");
                  //清除菜单数据
                  this.$store.commit('initRoutes',[]);
                  //跳转到首页
                  this.$router.replace("/");
              }
          })
         }else{
           this.putRequest("/api/teacher/updatePassword",this.passwords).then(resp => {
              if(resp && resp.code == 200){
                this.passwords = {
                  oldPassword: "",
                  newPassword: "",
                };
                this.confirmPassword = "";
                //退出登录
                //清空用户登录
                  window.sessionStorage.removeItem("tokenStr");
                  window.sessionStorage.removeItem("user");
                  //清除菜单数据
                  this.$store.commit('initRoutes',[]);
                  //跳转到首页
                  this.$router.replace("/");
              }
          })
         }
         
       }
    }
  }
}
</script>
